package com.yida.gwj.finance.controller;

import com.yida.common.core.domain.R;
import com.yida.common.core.enums.UserStatus;
import com.yida.common.core.utils.DateUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.InnerAuth;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.api.domain.GwjFinanceMoney;
import com.yida.gwj.common.constant.GwjConstants;
import com.yida.gwj.common.enums.GwjFinanceMoneyType;
import com.yida.gwj.finance.service.IGwjFinanceMoneyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author lqq
 * @date 2022-12-28
 */
@Api(tags = "缴存金额收入总帐明细")
@RestController
@RequestMapping("/money")
public class GwjFinanceMoneyController extends BaseController {

	@Resource
	private IGwjFinanceMoneyService financeMoneyService;

	@ApiOperation("查询缴存金额收入总帐明细列表")
	@GetMapping("/list")
	@RequiresPermissions("gwj:finance:money:list")
	public TableDataInfo list(GwjFinanceMoney financeMoney) {
		startPage();
		//查询未删除的
		financeMoney.setDelFlag(UserStatus.OK.getCode());
		List<GwjFinanceMoney> list = financeMoneyService.selectFinanceMoneyDetailList(financeMoney);
		return getDataTable(list);
	}

	@ApiOperation("导出缴存金额收入总帐明细列表")
	@PostMapping("/export")
	@RequiresPermissions("gwj:finance:money:export")
	@Log(title = "缴存金额收入总帐明细", businessType = BusinessType.EXPORT, systemName = "gwj")
	public void export(HttpServletResponse response, @RequestBody GwjFinanceMoney financeMoney) {
		//查询未删除的
		financeMoney.setDelFlag(UserStatus.OK.getCode());
		List<GwjFinanceMoney> list = financeMoneyService.selectFinanceMoneyDetailList(financeMoney);
		ExcelUtil<GwjFinanceMoney> util = new ExcelUtil<GwjFinanceMoney>(GwjFinanceMoney.class);
		util.exportExcel(response, list, "分户旧数据_" + DateUtils.dateTimeNow());
	}

	@ApiOperation("获取缴存金额收入总帐明细详细信息")
	@GetMapping(value = "/{moneyId}")
	@RequiresPermissions("gwj:finance:money:query")
	public AjaxResult getInfo(@PathVariable("moneyId") Long moneyId) {
		return AjaxResult.success(financeMoneyService.selectFinanceMoneyByMoneyId(moneyId));
	}

	@ApiOperation("新增缴存金额收入总帐明细")
	@PostMapping
	@RequiresPermissions("gwj:finance:money:add")
	@Log(title = "缴存金额收入总帐明细", businessType = BusinessType.INSERT, systemName = "gwj")
	public AjaxResult add(@Validated @RequestBody GwjFinanceMoney financeMoney) {
		financeMoney.setDivisionType(GwjFinanceMoneyType.HOUSEHOLD.getCode());
		return toAjax(financeMoneyService.insertFinanceMoney(financeMoney));
	}

	@ApiOperation("修改缴存金额收入总帐明细")
	@PutMapping
	@RequiresPermissions("gwj:finance:money:edit")
	@Log(title = "缴存金额收入总帐明细", businessType = BusinessType.UPDATE, systemName = "gwj")
	public AjaxResult edit(@Validated @RequestBody GwjFinanceMoney financeMoney) {
		if (financeMoneyService.selectFinanceMoneyByMoneyId(financeMoney.getMoneyId()).getSelectFlag().equals(GwjConstants.YES)) {
			return AjaxResult.error("该数据已经分户处理，不能进行修改！");
		}
		return toAjax(financeMoneyService.updateFinanceMoney(financeMoney));
	}

	@ApiOperation("删除缴存金额收入总帐明细")
	@DeleteMapping("/{moneyIds}")
	@RequiresPermissions("gwj:finance:money:remove")
	@Log(title = "缴存金额收入总帐明细", businessType = BusinessType.DELETE, systemName = "gwj")
	public AjaxResult remove(@PathVariable Long[] moneyIds) {
		return toAjax(financeMoneyService.deleteFinanceMoneyByMoneyIds(moneyIds));
	}

	/**
	 * 获取缴存金额收入总帐明细详细信息
	 */
	@InnerAuth
	@GetMapping("/inner/{moneyId}")
	public R<GwjFinanceMoney> selectFinanceMoneyByMoneyId(@PathVariable("moneyId") Long moneyId) {
		return R.ok(financeMoneyService.selectFinanceMoneyByMoneyId(moneyId));
	}

	/**
	 * 查询缴存金额收入总帐明细列表
	 */
	@InnerAuth
	@PostMapping("/inner/list")
	public R<List<GwjFinanceMoney>> selectFinanceMoneyList(@RequestBody GwjFinanceMoney financeMoney) {
		return R.ok(financeMoneyService.selectFinanceMoneyList(financeMoney));
	}

	/**
	 * 修改缴存金额收入总帐明细
	 */
	@InnerAuth
	@PutMapping("/inner")
	public R<Integer> updateFinanceMoney(@Validated @RequestBody GwjFinanceMoney financeMoney) {
		return R.ok(financeMoneyService.updateFinanceMoney(financeMoney));
	}

	/**
	 * 缴存金额收入总帐明细清空绑定的业务信息
	 *
	 * @param financeMoney 缴存金额收入总帐明细
	 * @return 结果
	 */
	@InnerAuth
	@PutMapping("/inner/clear")
	public R<Integer> clearFinanceMoneyBizInfo(@Validated @RequestBody GwjFinanceMoney financeMoney) {
		return R.ok(financeMoneyService.clearFinanceMoneyBizInfo(financeMoney));
	}
}
